﻿using System;
using System.Collections.Generic;
using System.Linq;

using Ue.Eagle.Runtime.Folders;
using Ue.Eagle.Runtime.Models;

namespace Ue.Eagle.Runtime
{
    public class PluginManager : IPluginManager
    {
        private readonly IEnumerable<IPluginFolder> folders;

        public PluginManager(IEnumerable<IPluginFolder> folders)
        {
            this.folders = folders;
        }

        public IEnumerable<PluginDescriptor> AvailablePlugins()
        {
            return this.folders.SelectMany(folder => folder.AvailablePlugins());
        }

        public IEnumerable<PluginExtensionPoint> AvailableExtensionPoints()
        {
            return this.AvailablePlugins().SelectMany(plugin => plugin.ExtensionPoints);
        }

        public IEnumerable<PluginExtension> AvailableExtensions()
        {
            return this.AvailablePlugins().SelectMany(plugin => plugin.Extensions);
        }

        public PluginDescriptor GetPlugin(string pluginId)
        {
            return this.AvailablePlugins().FirstOrDefault(plugin => plugin.Id.Equals(pluginId));
        }

        public IEnumerable<PluginExtension> GetExtensions(string pointId)
        {
            return this.AvailableExtensions().Where(extension => extension.PointId.Equals(pointId));
        }

        public PluginExtensionPoint GetExtensionPoint(string pointId)
        {
            return this.AvailableExtensionPoints().FirstOrDefault(point => point.Id.Equals(pointId));
        }
    }
}
